package com.example.seckill.Controller;


import com.example.seckill.Exception.ErrorEnum;
import com.example.seckill.Exception.GlobalException;
import com.example.seckill.VO.EventRequest;
//import com.example.seckill.annotation.RoleLimit;
import com.example.seckill.common.CommonResult;
import com.example.seckill.mapper.EventMapper;
import com.example.seckill.mapper.RoomMapper;
import com.example.seckill.pojo.Event;
import com.example.seckill.VO.EventVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;



@RestController
@RequestMapping("/event")
public class EventController {
    @Autowired
    EventMapper eventMapper;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    RoomMapper roomMapper;


    @GetMapping("getLastEvent")
    public CommonResult<EventVO> getLastEvent(){
        Event foundEvent=eventMapper.selectLastEvent();
        if (foundEvent==null) throw new GlobalException(ErrorEnum.NO_EVENT_FOUND);
        EventVO eventVO=new EventVO();

        //在redis中查找
        String start="StartTime";
        String end="endTime";
        Date startTime=(Date) redisTemplate.opsForValue().get(start);
        Date endTime=(Date) redisTemplate.opsForValue().get(end);
        if(startTime==null)
            startTime=foundEvent.getStartTime();
        if(endTime==null)
            endTime=foundEvent.getEndTime();

        Date nowDate=new Date();
        Integer remainSec=0;
        Integer status=0;

        //如果有一为空直接报错
        if(startTime==null||endTime==null) throw new GlobalException(ErrorEnum.NO_EVENT_FOUND);

        else if(startTime.after(nowDate)){
            //未开始
            status=0;
            remainSec= (int) ((startTime.getTime() - nowDate.getTime()) / 1000);

        }else if(startTime.before(nowDate)&&endTime.after(nowDate)){
            //进行中
            status=1;
            remainSec=0;
        }else {
            //已结束
            status=2;
            remainSec=-1;
        }

        eventVO.setStartTime(startTime);
        eventVO.setEndTime(endTime);
        eventVO.setStatus(status);
        eventVO.setEvent_name(foundEvent.getEvent_name());
        eventVO.setRemainSeconds(remainSec);
        return CommonResult.success(eventVO);
    }


    @PostMapping("/createEvent")
    public CommonResult createEvent(@RequestBody @Validated EventRequest event){

        Event newEvent=new Event();
        Date startTime=event.getStart_time();
        Date endTime=event.getEnd_time();
        if(endTime.before(startTime)||startTime.before(new Date())){

            throw new GlobalException(ErrorEnum.TIME_ILLEGAL);
        }
        newEvent.setEvent_name(event.getEvent_name());
        newEvent.setStartTime(event.getStart_time());
        newEvent.setEndTime(event.getEnd_time());
        System.out.println("event_name:"+newEvent.getEvent_name());


        eventMapper.create(newEvent);
        Long event_id=newEvent.getId();
        if(event_id==null) return CommonResult.error(ErrorEnum.ERROR);
        //手工设置，不匹配时间
//        List<SecKillRoom> list=event.getRooms();
//        for(SecKillRoom room:list){
//            room.setEvent_id(event_id);
//            roomMapper.insertSeckillRoom(room);
//
//        }
        return CommonResult.success("插入成功");



    }

    @GetMapping("getAllEvent")
    public CommonResult  getAllEvent(){
        List<EventVO>list=eventMapper.getAllEvent();
        if(list.isEmpty())
        throw new GlobalException(ErrorEnum.NO_EVENT_FOUND);
        for(EventVO  event:list){
            Date startTime=event.getStartTime();
            Date endTime=event.getEndTime();

//            System.out.println(1);
            Date nowDate=new Date();
            Integer status=0;
            Integer remainSec=0;
        if(startTime.after(nowDate)){
            //未开始
            status=0;
            remainSec= (int) ((startTime.getTime() - nowDate.getTime()) / 1000);

        }else if(startTime.before(nowDate)&&endTime.after(nowDate)){
            //进行中
            status=1;
            remainSec=0;
        }else {
            //已结束
            status=2;
            remainSec=-1;
        }
        event.setStatus(status);
        event.setRemainSeconds(remainSec);
        }
        return CommonResult.success(list);

    }

//    @GetMapping("/getRoomsByEventID/{event_id}")
//    public CommonResult GetRoomsByEventID(@PathVariable Long event_id){
//        if(event_id==null) return CommonResult.error(ErrorEnum.REQUEST_ILLEGAL);
//        List<SecKillRoom>res=roomMapper.getRoomsByEventID(event_id);
//        if(res.isEmpty()) System.out.println("数据为空");
//        return CommonResult.success(res);
//    }

}
